
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>字典对象 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="集合对象" href="set.html" />
    <link rel="prev" title="列表对象" href="list.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/c-api/dict.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="set.html" title="集合对象"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="list.html" title="列表对象"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API 参考手册</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">具体的对象层</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="dictionary-objects">
<span id="dictobjects"></span><h1>字典对象<a class="headerlink" href="#dictionary-objects" title="永久链接至标题">¶</a></h1>
<span class="target" id="index-0"></span><dl class="type">
<dt id="c.PyDictObject">
<code class="sig-name descname">PyDictObject</code><a class="headerlink" href="#c.PyDictObject" title="永久链接至目标">¶</a></dt>
<dd><p>这个 <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> 的子类型代表一个Python字典对象。</p>
</dd></dl>

<dl class="var">
<dt id="c.PyDict_Type">
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a> <code class="sig-name descname">PyDict_Type</code><a class="headerlink" href="#c.PyDict_Type" title="永久链接至目标">¶</a></dt>
<dd><p>Python字典类型表示为 <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> 的实例。这与Python层面的 <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 是相同的对象。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Check">
int <code class="sig-name descname">PyDict_Check</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Check" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <em>p</em> 是字典对象或者字典类型的子类型的实例，则返回真。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_CheckExact">
int <code class="sig-name descname">PyDict_CheckExact</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_CheckExact" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <em>p</em> 是字典对象但不是字典类型的子类型的实例，则返回真。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_New">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_New</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_New" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回一个新的空字典，失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDictProxy_New">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDictProxy_New</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *mapping</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDictProxy_New" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回 <a class="reference internal" href="../library/types.html#types.MappingProxyType" title="types.MappingProxyType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.MappingProxyType</span></code></a> 对象，用于强制执行只读行为的映射。这通常用于创建视图以防止修改非动态类类型的字典。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Clear">
void <code class="sig-name descname">PyDict_Clear</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Clear" title="永久链接至目标">¶</a></dt>
<dd><p>清空现有字典的所有键值对。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Contains">
int <code class="sig-name descname">PyDict_Contains</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Contains" title="永久链接至目标">¶</a></dt>
<dd><p>确定 <em>key</em> 是否包含在字典 <em>p</em> 中。如果 <em>key</em> 匹配上 <em>p</em> 的某一项，则返回 <code class="docutils literal notranslate"><span class="pre">1</span></code> ，否则返回 <code class="docutils literal notranslate"><span class="pre">0</span></code> 。返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 表示出错。这等同于Python表达式 <code class="docutils literal notranslate"><span class="pre">key</span> <span class="pre">in</span> <span class="pre">p</span></code> 。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Copy">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_Copy</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Copy" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回与 <em>p</em> 包含相同键值对的新字典。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_SetItem">
int <code class="sig-name descname">PyDict_SetItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *val</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_SetItem" title="永久链接至目标">¶</a></dt>
<dd><p>使用 <em>key</em> 作为键将 <em>value</em> 插入字典 <em>p</em> 。 <em>key</em> 必须为 <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> ；如果不是，会抛出 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> 异常。成功返回 <code class="docutils literal notranslate"><span class="pre">0</span></code> ，失败返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_SetItemString">
int <code class="sig-name descname">PyDict_SetItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, const char<em> *key</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *val</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_SetItemString" title="永久链接至目标">¶</a></dt>
<dd><p id="index-1">使用 <em>key</em> 作为键将 <em>value</em> 插入到字典 <em>p</em> 中。<em>key</em> 必须为 <code class="xref c c-type docutils literal notranslate"><span class="pre">const</span> <span class="pre">char*</span></code>。键对象是由 <code class="docutils literal notranslate"><span class="pre">PyUnicode_FromString(key)</span></code> 创建的。成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>，失败时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_DelItem">
int <code class="sig-name descname">PyDict_DelItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_DelItem" title="永久链接至目标">¶</a></dt>
<dd><p>使用键 <em>key</em> 删除字典 <em>p</em> 中的条目。 <em>key</em> 必须是可哈希的；如果不是，则抛出 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> 异常。成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code> ，失败时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_DelItemString">
int <code class="sig-name descname">PyDict_DelItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, const char<em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_DelItemString" title="永久链接至目标">¶</a></dt>
<dd><p>删除字典 <em>p</em> 中由字符串 <em>key</em> 作为键的条目。成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code> ，失败时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_GetItem">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_GetItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_GetItem" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>从字典 <em>p</em> 中返回以 <em>key</em> 为键的对象。 如果键名 <em>key</em> 不存在但 <em>没有</em> 设置一个异常则返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。</p>
<p>需要注意的是，调用 <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> 和 <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code></a> 方法产生的异常不会被抛出。改用 <a class="reference internal" href="#c.PyDict_GetItemWithError" title="PyDict_GetItemWithError"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyDict_GetItemWithError()</span></code></a> 获得错误报告。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_GetItemWithError">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_GetItemWithError</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_GetItemWithError" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p><a class="reference internal" href="#c.PyDict_GetItem" title="PyDict_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyDict_GetItem()</span></code></a> 的变种，它不会屏蔽异常。 当异常发生时将返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> <strong>并且</strong> 设置一个异常。 如果键不存在则返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> <strong>并且不会</strong> 设置一个异常。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_GetItemString">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_GetItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, const char<em> *key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_GetItemString" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>这与 <a class="reference internal" href="#c.PyDict_GetItem" title="PyDict_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyDict_GetItem()</span></code></a> 一样，但是 <em>key</em> 需要指定一个 <code class="xref c c-type docutils literal notranslate"><span class="pre">const</span> <span class="pre">char*</span></code> ，而不是 <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject*</span></code></a> 。</p>
<p>需要注意的是，调用 <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> 、 <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code></a> 方法和创建一个临时的字符串对象时产生的异常不会被抛出。改用 <a class="reference internal" href="#c.PyDict_GetItemWithError" title="PyDict_GetItemWithError"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyDict_GetItemWithError()</span></code></a> 获得错误报告。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_SetDefault">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_SetDefault</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *key</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *defaultobj</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_SetDefault" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>这跟Python层面的 <a class="reference internal" href="../library/stdtypes.html#dict.setdefault" title="dict.setdefault"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.setdefault()</span></code></a> 一样。如果键 <em>key</em> 存在，它返回在字典 <em>p</em> 里面对应的值。如果键不存在，它会和值 <em>defaultobj</em> 一起插入并返回 <em>defaultobj</em> 。这个函数只计算 <em>key</em> 的哈希函数一次，而不是在查找和插入时分别计算它。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.4 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Items">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_Items</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Items" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回一个包含字典中所有键值项的 <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyListObject</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Keys">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_Keys</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Keys" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回一个包含字典中所有键(keys)的 <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyListObject</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Values">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyDict_Values</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Values" title="永久链接至目标">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回一个包含字典中所有值(values)的 <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyListObject</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Size">
Py_ssize_t <code class="sig-name descname">PyDict_Size</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Size" title="永久链接至目标">¶</a></dt>
<dd><p id="index-2">返回字典中项目数，等价于对字典 <em>p</em> 使用 <code class="docutils literal notranslate"><span class="pre">len(p)</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Next">
int <code class="sig-name descname">PyDict_Next</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em>, Py_ssize_t<em> *ppos</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> **pkey</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> **pvalue</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Next" title="永久链接至目标">¶</a></dt>
<dd><p>迭代字典 <em>p</em> 中的所有键值对。 在第一次调用此函数开始迭代之前，由 <em>ppos</em> 所引用的 <code class="xref c c-type docutils literal notranslate"><span class="pre">Py_ssize_t</span></code> 必须初始化为 <code class="docutils literal notranslate"><span class="pre">0</span></code>；该函数将为字典中的每个键值对返回真值，一旦所有键值对报告完毕则返回假值。 形参 <em>pkey</em> 和 <em>pvalue</em> 应当指向 <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject*</span></code></a> 变量，它们将分别使用每个键和值来填充，或者也可以为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 通过它们返回的任何引用都是暂借的。 <em>ppos</em> 在迭代期间不应被更改。 它的值表示内部字典结构中的偏移量，并且由于结构是稀疏的，因此偏移量并不连续。</p>
<p>例如</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span> <span class="o">*</span><span class="n">key</span><span class="p">,</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
<span class="n">Py_ssize_t</span> <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

<span class="k">while</span> <span class="p">(</span><span class="n">PyDict_Next</span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">dict</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pos</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">key</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span>
    <span class="cm">/* do something interesting with the values... */</span>
    <span class="p">...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>字典 <em>p</em> 不应该在遍历期间发生改变。在遍历字典时，改变键中的值是安全的，但仅限于键的集合不发生改变。例如:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span> <span class="o">*</span><span class="n">key</span><span class="p">,</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
<span class="n">Py_ssize_t</span> <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

<span class="k">while</span> <span class="p">(</span><span class="n">PyDict_Next</span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">dict</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pos</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">key</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span>
    <span class="kt">long</span> <span class="n">i</span> <span class="o">=</span> <span class="n">PyLong_AsLong</span><span class="p">(</span><span class="n">value</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">PyErr_Occurred</span><span class="p">())</span> <span class="p">{</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">PyObject</span> <span class="o">*</span><span class="n">o</span> <span class="o">=</span> <span class="n">PyLong_FromLong</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">o</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">PyDict_SetItem</span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">dict</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">o</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Merge">
int <code class="sig-name descname">PyDict_Merge</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *b</em>, int<em> override</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Merge" title="永久链接至目标">¶</a></dt>
<dd><p>对映射对象 <em>b</em> 进行迭代，将键值对添加到字典 <em>a</em>。 <em>b</em> 可以是一个字典，或任何支持 <a class="reference internal" href="mapping.html#c.PyMapping_Keys" title="PyMapping_Keys"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMapping_Keys()</span></code></a> 和 <a class="reference internal" href="object.html#c.PyObject_GetItem" title="PyObject_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetItem()</span></code></a> 的对象。 如果 <em>override</em> 为真值，则如果在 <em>b</em> 中找到相同的键则 <em>a</em> 中已存在的相应键值对将被替换，否则如果在 <em>a</em> 中没有相同的键则只是添加键值对。 当成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code> 或者当引发异常时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_Update">
int <code class="sig-name descname">PyDict_Update</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *b</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Update" title="永久链接至目标">¶</a></dt>
<dd><p>这与 C 中的 <code class="docutils literal notranslate"><span class="pre">PyDict_Merge(a,</span> <span class="pre">b,</span> <span class="pre">1)</span></code> 一样，也类似于 Python 中的 <code class="docutils literal notranslate"><span class="pre">a.update(b)</span></code>，差别在于 <a class="reference internal" href="#c.PyDict_Update" title="PyDict_Update"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyDict_Update()</span></code></a> 在第二个参数没有 &quot;keys&quot; 属性时不会回退到迭代键值对的序列。 当成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code> 或者当引发异常时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_MergeFromSeq2">
int <code class="sig-name descname">PyDict_MergeFromSeq2</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *seq2</em>, int<em> override</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_MergeFromSeq2" title="永久链接至目标">¶</a></dt>
<dd><p>将 <em>seq2</em> 中的键值对更新或合并到字典 <em>a</em>。 <em>seq2</em> 必须为产生长度为 2 的用作键值对的元素的可迭代对象。 当存在重复的键时，如果 <em>override</em> 真值则最后出现的键胜出。 当成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code> 或者当引发异常时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。 等价的 Python 代码（返回值除外）:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">def</span> <span class="n">PyDict_MergeFromSeq2</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">seq2</span><span class="p">,</span> <span class="n">override</span><span class="p">)</span><span class="o">:</span>
    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="n">in</span> <span class="nl">seq2</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">override</span> <span class="n">or</span> <span class="n">key</span> <span class="n">not</span> <span class="n">in</span> <span class="nl">a</span><span class="p">:</span>
            <span class="n">a</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="c.PyDict_ClearFreeList">
int <code class="sig-name descname">PyDict_ClearFreeList</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_ClearFreeList" title="永久链接至目标">¶</a></dt>
<dd><p>清空释放列表。 返回所释放的条目数。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="list.html"
                        title="上一章">列表对象</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="set.html"
                        title="下一章">集合对象</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/c-api/dict.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="set.html" title="集合对象"
             >下一页</a> |</li>
        <li class="right" >
          <a href="list.html" title="列表对象"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API 参考手册</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="concrete.html" >具体的对象层</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>